package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart) {
        //    1.判断购物车是否存在该菜品或套餐
        //    select * from shpping_cart where user_id=? and dish_id=?
        //    select * from shpping_cart where user_id=? and setmeal_id=?
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        queryWrapper.eq(shoppingCart.getDishId() != null, "dish_id", shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId());
        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);

        if(cart==null){
        //    2.1若不存在,执行添加操作,在shopping_car表中添加一条数据//userID和时间没有，数量要设置为
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
        }else{
        //    2.2若存在，执行更新操作，将number++
            cart.setNumber(cart.getNumber()+1);
            shoppingCartService.updateById(cart);
        }
        return R.success("添加成功");
    }

    /**
     * 查询购物车
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
    //    select * from ShoppingCart where user_id=?
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper=new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("user_id",BaseContext.getCurrentId());
        shoppingCartQueryWrapper.orderByDesc("create_time");
        List<ShoppingCart> list = shoppingCartService.list(shoppingCartQueryWrapper);
        return R.success(list);
    }
    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
    //    delete * from ShoppingCart where user_id=?
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper=new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("user_id",BaseContext.getCurrentId());
        shoppingCartService.remove(shoppingCartQueryWrapper);
        return R.success("删除成功");
    }
    /**
     * 删除购物车的一项
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
    //    delete * from shoppingCart where setmeal_id=?
    //    delete * from shoppingCart where dish_id=?
        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper=new QueryWrapper<>();
        shoppingCartQueryWrapper.eq(shoppingCart.getSetmealId()!=null,"setmeal_id",shoppingCart.getSetmealId());
        shoppingCartQueryWrapper.eq(shoppingCart.getDishId()!=null,"dish_id",shoppingCart.getDishId());
        ShoppingCart shoppingCart1 = shoppingCartService.getOne(shoppingCartQueryWrapper);
        if(shoppingCart1.getNumber()<=1){
        shoppingCartService.remove(shoppingCartQueryWrapper);
        }else{
            shoppingCart1.setNumber(shoppingCart1.getNumber()-1);
            shoppingCartService.updateById(shoppingCart1);
        }
       return R.success("删除成功");
    }
}
